package com.example.demo02.controller.Exception;

import com.example.demo02.entity.Goods;
import com.example.demo02.dao.GoodsDao;
import com.example.demo02.utils.ResponseData;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Tag(name = "异常测试接口")
@RestController
@RequestMapping("/exception")
public class ExceptionController {
    @GetMapping("/test1")
    public ResponseData test1() {
        int[] arr = {1,2,3};
        arr = null;
        System.out.println(arr[0]); //空指针异常
        Map<Integer, Goods> goodsMap = GoodsDao.findAllGoods();
        return new ResponseData("200", null, true, goodsMap);
    }

    @GetMapping("/test2")
    public ResponseData test2() {
        int[] arr = {1,2,3};
        System.out.println(arr[3]); //数组下标越界异常
        Map<Integer, Goods> goodsMap = GoodsDao.findAllGoods();
        return new ResponseData("200", null, true, goodsMap);
    }

    @GetMapping("/test3")
    public ResponseData test3() {
        int[] arr = {1,2,3};
        int i = 10/0; //算术异常
        Map<Integer, Goods> goodsMap = GoodsDao.findAllGoods();
        return new ResponseData("200", null, true, goodsMap);
    }

}
